package com.ctg.behavior.calc.impala.builder.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.event.CtCalcEventMeasureReport;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtAggregatorConsts;
import com.ctg.behavior.common.consts.CtFieldConsts;

@Component
public class CtEventMeasureGroupByBuilder {

	/*************************************************************
	 * @apiNote 此处的Group By的拼接要考虑到Select部分：两部分要匹配
	 * @param measure
	 * @return
	 */
	public String doBuilder(CtCalcEventMeasureReport measure) {

		StringBuilder sbuf = new StringBuilder();

		// 总和 均值 最大值.. 时出现事件类型 field:event.$Anything.$user_id
		String field = measure.getField();
		CtCalcPropertyType selectFieldProperty = CtCalcPropertyUtils.getValueType(field);
		// byFields (查询分组字段)
		List<String> byFields = measure.getByFields();
		List<String> byFieldsProperties = new ArrayList<>();
		for (String oneByField : byFields) {
			CtCalcPropertyType byFieldProperty = CtCalcPropertyUtils.getValueType(oneByField);
			String propField = byFieldProperty.getProperty();
			byFieldsProperties.add(propField);
		}
		// 公共GROUP BY字段：第一层、第二层公共部分
		StringBuilder byFieldsGroupBy = new StringBuilder();
		for (int index = 0; index < byFieldsProperties.size(); index++) {
			if (index > 0) {
				byFieldsGroupBy.append(" , ");
			}
			byFieldsGroupBy.append(byFieldsProperties.get(index));
		}

		switch (measure.getAggregator().toLowerCase()) {
		// 总次数
		case CtAggregatorConsts.KEY_GENERAL:
			// 过去7天总次数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_7:
			// 过去30天总次数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_30:
			// 当月总次数
		case CtAggregatorConsts.KEY_COUNT_CUR_MONTH:
			// 合计总次数
		case CtAggregatorConsts.KEY_ROLLUP_COUNT:

			// 总和 均值 最大值 最小值
		case CtAggregatorConsts.KEY_SUM:
		case CtAggregatorConsts.KEY_AVG:
		case CtAggregatorConsts.KEY_MAX:
		case CtAggregatorConsts.KEY_MIN:

			if (CtFieldConsts.FIELD_ANYTHING.equalsIgnoreCase(measure.getEventId())) {
				sbuf.append(" GROUP BY byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
			} else {
				sbuf.append(" GROUP BY ").append(" event , byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
			}
			return sbuf.toString();

		// 用户数
		case CtAggregatorConsts.KEY_UNIQUE:
			// 平均数
		case CtAggregatorConsts.KEY_AVERAGE:
			// 过去7天用户数
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_7:
			// 过去30天用户数
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_30:
			// 当月用户数
		case CtAggregatorConsts.KEY_UNIQ_CUR_MONTH:
			// 合计用户数
		case CtAggregatorConsts.KEY_ROLLUP_UNIQ:

			if (CtFieldConsts.FIELD_ANYTHING.equalsIgnoreCase(measure.getEventId())) {
				// 第二层
				sbuf.append(" GROUP BY byTime ");
				sbuf.append(" , user_id ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				// 第一层
				sbuf.append(" GROUP BY ").append(" T1.byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
			} else {
				// 第二层
				sbuf.append(" GROUP BY byTime ");
				sbuf.append(" , event ").append(", user_id ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				// 第一层
				sbuf.append(" GROUP BY T1.event , T1.byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
			}
			return sbuf.toString();

		// 去重数
		case CtAggregatorConsts.KEY_UNIQCOUNT:
			if (CtFieldConsts.FIELD_ANYTHING.equalsIgnoreCase(measure.getEventId())) {
				// 第二层
				sbuf.append(" GROUP BY byTime , ");
				sbuf.append(selectFieldProperty.getProperty());
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				sbuf.append(" GROUP BY T1.byTime ");
				// 第一层
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(byFieldsGroupBy.toString());
				}
			} else {
				// 第二层
				sbuf.append(" GROUP BY ").append(" event , byTime ");
				sbuf.append(" , ").append(selectFieldProperty.getProperty());
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				// 第一层
				sbuf.append(" GROUP BY T1.event , T1.byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsGroupBy.toString());
				}
			}
			return sbuf.toString();

		// 人均值
		case CtAggregatorConsts.KEY_UNIQAVG:
			if (CtFieldConsts.FIELD_ANYTHING.equalsIgnoreCase(measure.getEventId())) {
				// 第二层
				sbuf.append(" GROUP BY ").append("user_id , byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(", ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				// 第一层
				sbuf.append(" GROUP BY T1.byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(byFieldsGroupBy.toString());
				}
			} else {
				// 第二层
				sbuf.append(" GROUP BY ").append(" user_id,  event , byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(", ").append(byFieldsGroupBy.toString());
				}
				sbuf.append(" ) T1 ");
				// 第一层
				sbuf.append(" GROUP BY T1.event , T1.byTime ");
				if (!byFieldsProperties.isEmpty()) {
					sbuf.append(", ").append(byFieldsGroupBy.toString());
				}
			}
			return sbuf.toString();
		}

		return "";

	}

}
